package com.tgy.controller;

import com.tgy.dao.DeptDao;
import com.tgy.entity.Dept;
import com.tgy.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/***
 * @ClassName: DeptController
 * @Description:
 * @Auther: 送一一块二向箔
 * @Date: 20:07 2022/1/10
 * @version : V1.0
 */
@RestController
@Slf4j
public class DeptController {
    @GetMapping("/test")
    public String test(){
        log.info("大家好");
        return "6666";
    }

    @Resource
    DeptDao deptDao;

    @GetMapping("/id/{id}")
    public Dept id(@PathVariable(name = "id", required = true) Long id ){
        return deptDao.getDeptById(id);
    };

    @RequestMapping("/all")
    public List<Dept> all(){
        return deptDao.all();
    }

    @PostMapping("/get")
    public List<Dept> getByLimit(@RequestBody Map<String, String> inMap){
        Dept dept = new Dept();

        log.info(String.valueOf("".equals(inMap.get("deptNo"))));
        if (inMap.get("deptNo")!=null && !"".equals(inMap.get("deptNo"))){
            dept.setDeptNo(Long.valueOf(inMap.get("deptNo")));
        }
        dept.setDeptName(inMap.get("deptName"));
        dept.setDbSource(inMap.get("dbSource"));
        log.info("执行按条件查询");
        List<Dept> all = deptDao.getAll(dept);
        return all;
    }

    @PostMapping("/list")
    public List<Dept> getByLimit(@RequestBody Dept dept){
        List<Dept> all = deptDao.getAll(dept);
        return all;
    }

    @Resource
    DeptService deptService;
    @PostMapping("/add")
    public Boolean add(@RequestBody Dept dept){
        log.info("增加操作{}",dept);
        Boolean aBoolean = deptService.addDept(dept);
        return aBoolean;
    }

    @PostMapping("/upd")
    public Integer upd(@RequestBody Dept dept){
        log.info("修改操作{}",dept);
        Integer upd = deptDao.upd(dept);
        return upd;

    }
    @GetMapping("/del")
    public Integer del(@RequestParam(name = "id", required = true) Long id ){
        log.info("删除操作{}",id);
        return deptDao.del(id);
    }

    @GetMapping("/delR/{id}")
    public Integer delR(@PathVariable Long id ){
        log.info("RestFul删除操作{}",id);
        return deptDao.del(id);
    }

    /**
     * DiscoveryClient 可以用来获取一些配置的信息，得到具体的微服务！
     */
    @Resource
    private DiscoveryClient client;
    /**
     * 获取一些注册进来的微服务的信息~，
     *
     * @return
     */
    @GetMapping("/dept/discovery")
    public Object discovery() {
        // 获取微服务列表的清单
        List<String> services = client.getServices();
        System.out.println("discovery=>services:" + services);
        // 得到一个具体的微服务信息,通过具体的微服务id，在配置文件中配置的applicationName
        List<ServiceInstance> instances = client.getInstances("PROVIDERDEPT01");
        for (ServiceInstance instance : instances) {
            System.out.println(
                    instance.getHost() + "\t" + // 主机名称
                            instance.getPort() + "\t" + // 端口号
                            instance.getUri() + "\t" + // uri
                            instance.getServiceId() // 服务id
            );
        }
        return this.client;
    }

}
